﻿using System;
using Business;
using EntitiesTM;
using System.Data;

namespace TimeWork
{
    public partial class CreateNewStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["email"] == null)
            {
                Response.Redirect("http://"+Request.Url.Authority+"/Login.aspx");
            }
            if (!IsPostBack)
            {
                LoadContent();
            }
        }

        public void LoadContent()
        {
            ddlPermission.DataSource = BuPermission.selAllPermission(Session["companyid"].ToString());
            ddlPermission.DataTextField = "PermissionName";
            ddlPermission.DataValueField = "PermissionID";
            ddlPermission.DataBind();

            DataTable dataResultUnit = BuResultUnit.selAllResultUnit(Session["companyid"].ToString());

            ddlResultUnit.DataSource = dataResultUnit;
            ddlResultUnit.DataTextField = "ResultUnitName";
            ddlResultUnit.DataValueField = "ResultUnitID";
            ddlResultUnit.DataBind();
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            lblEmail.Text = String.Empty;
            string email = txtEmail.Text.Trim();
            string name = txtName.Text.Trim();
            string password = txtPassword.Text.Trim();
            string confirm = txtPasswordConfirm.Text.Trim();
            txtEmail.CssClass = "input-error";
            txtName.CssClass = "input-error";
            txtPassword.CssClass = "input-error";
            txtPasswordConfirm.CssClass = "input-error";
            // Kiem tra khac rong
            if (!email.Equals(""))
            {
                txtEmail.CssClass = "valid";
            }
            if (!name.Equals(""))
            {
                txtName.CssClass = "valid";
            }
            if (password.Equals(confirm))
            {
                txtPassword.CssClass = "valid";
                txtPasswordConfirm.CssClass = "valid";
            }


            if (!email.Equals(""))
            {
                if (!name.Equals(""))
                {
                    if (password.Equals(confirm))
                    {
                        // Kiem tra email ton tai
                        int[] check = BuStaff.checkEmail(email);
                        if (check[0] == 1)
                        {
                            if (ddlResultUnit.SelectedValue != "0")
                            {
                                EnStaff staff = new EnStaff();
                                staff.staffName = name;
                                staff.staffPhone = txtPhone.Text.Trim();
                                staff.staffCompanyID = Int32.Parse(Session["companyid"].ToString());
                                try
                                {
                                    staff.staffBirthday = DateTime.ParseExact(txtBirthday.Text, "dd/MM/yyyy", null);
                                }
                                catch
                                {
                                    staff.staffBirthday = DateTime.Parse("1/1/1754");
                                }


                                staff.staffGender = Boolean.Parse(ddlGender.SelectedValue);
                                staff.staffAddress = txtAddress.Text.Trim();
                                staff.staffPosition = txtPosition.Text.Trim();
                                staff.staffEmail = email;
                                staff.staffScale = txtScale.Text.Trim();
                                staff.staffLevel = txtLevel.Text.Trim().Equals("") ? 0 : Int32.Parse(txtLevel.Text.Trim());
                                staff.staffPermissionID = Int32.Parse(ddlPermission.SelectedValue);
                                staff.staffPassword = Utilities.UtiGeneralClass.Md5Encode(txtPassword.Text.Trim());
                                staff.staffResultUnitID = Int32.Parse(ddlResultUnit.SelectedValue);

                                BuStaff.insStaff(staff, Session["companyid"].ToString());
                                Response.Redirect("ListStaff.aspx");
                            }
                            else
                            {
                                lblEmail0.Text = "<br>Vui lòng chọn phòng ban, nếu chưa có, hãy tạo mới!";
                            }
                        }
                        else
                        {
                            lblEmail.Text = "Email đã tồn tại!";
                        }
                    }
                }
            }
        }
    }
}